package com.bang.study.esl.listener;

import com.alibaba.fastjson.JSONObject;
import com.bang.study.esl.model.enums.EventTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.freeswitch.esl.client.internal.Context;
import org.freeswitch.esl.client.internal.IModEslApi;
import org.freeswitch.esl.client.outbound.IClientHandler;
import org.freeswitch.esl.client.outbound.IClientHandlerFactory;
import org.freeswitch.esl.client.transport.CommandResponse;
import org.freeswitch.esl.client.transport.SendMsg;
import org.freeswitch.esl.client.transport.event.EslEvent;
import org.freeswitch.esl.client.transport.message.EslHeaders;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Auther: Bang
 * @Date: 2019/9/20 0020 10:23
 * @Description:
 */
@Slf4j
@Component
public class EslOutBoundListener implements IClientHandlerFactory {


    private void bridge(Context context) {
        SendMsg msg = new SendMsg();
        msg.addCallCommand("execute");
        msg.addExecuteAppName("bridge");
        msg.addExecuteAppArg("user/2001");
        List<String> msgLines = msg.getMsgLines();
        CommandResponse commandResponse = context.sendMessage(msg);

        if (commandResponse.getReplyText().startsWith("+OK")) {
            System.out.println("Call bridge successful");
        } else {
            System.err.println(String.format("Call bridge failed: [{}}", commandResponse.getReplyText()));
        }
    }

    @Override
    public IClientHandler createClientHandler() {
        return new IClientHandler() {

            @Override
            public void onConnect(Context ctx, EslEvent event) {


                String eventName = event.getEventName();
                Map<String, String> eventHeaders = event.getEventHeaders();
                List<String> eventBodyLines = event.getEventBodyLines();
                Map<EslHeaders.Name, String> messageHeaders = event.getMessageHeaders();

                log.error("===================== OutBoundListener CONNECT =====================");
                log.error("eventName {}", eventName);
                log.error("eventHeaders {}", JSONObject.toJSONString(eventHeaders));
                log.error("eventBodyLines {}", JSONObject.toJSONString(eventBodyLines));
                log.error("messageHeaders {}", JSONObject.toJSONString(messageHeaders));
                CommandResponse commandResponse;
                log.error("===========================================================================");
                commandResponse =  ctx.setEventSubscriptions(IModEslApi.EventFormat.PLAIN, "CHANNEL_CREATE CHANNEL_PARK CHANNEL_ANSWER PLAYBACK_STOP DTMF CHANNEL_HANGUP CUSTOM callcenter::info asrmessage");
                commandResponse = ctx.sendMessage(new SendMsg().addCallCommand("execute").addExecuteAppName("bridge").addExecuteAppArg("user/2001").addEventLock());
                log.error("===========================================================================");
                log.error("commandResponse: {}",JSONObject.toJSONString(commandResponse));
                log.error("===================== OutBoundListener CONNECT =====================");


            }

            /**
             * Signal of a server initiated event.
             *
             * @param ctx
             * @param event as an {@link EslEvent}
             */
            @Override
            public void onEslEvent(Context ctx, EslEvent event) {
                log.error("===================== OutBoundListener onEslEvent =====================");
                log.error("event [{}]",JSONObject.toJSONString(event));
                EventTypeEnum eventName = EventTypeEnum.valueOf(event.getEventName());
                Map<String, String> eventHeaders = event.getEventHeaders();
                List<String> eventBodyLines = event.getEventBodyLines();
                Map<EslHeaders.Name, String> messageHeaders = event.getMessageHeaders();

                System.err.println("eventName = " + eventName);
                System.err.println(String.format("eventHeaders =[{}]", JSONObject.toJSONString(eventHeaders)));
                log.error("===================== OutBoundListener onEslEvent =====================");

                switch (eventName) {
                    case CHANNEL_ANSWER:
                        break;
                    case PLAYBACK_START:
                        break;
                    case PLAYBACK_STOP:
                        break;
                    case CHANNEL_BRIDGE:
                        break;
                    case CHANNEL_UNBRIDGE:
                        break;
                    case CHANNEL_HANGUP:
                        ctx.closeChannel();
                        break;
                }
            }
        };
    }
}
